<?php
namespace Admin\Controller;
use Common\Model\MongodbModel;
use Common\Model\OptionModel;
class OptionWriterController extends AdminController{
    /*用户资金*/
    public function index(){
        //读取配置
        $s_type = C('MAKER_MONEY_LOG_TYPE');
        $s_types = array_flip($s_type);
        $this->assign('s_types',$s_types);
        //实例化MongoDB
        $Model = new OptionModel('maker_log');  
        //状态搜索
        if($_REQUEST['status'] && $_REQUEST['status']!=-2){
            $map['money_type']=intval($_REQUEST['status']);
            $status = $_REQUEST['status'];
        }
        else{
            $status=-2;
        }
       $this->assign('status',$status); 
        //做市商名称搜索
        if($_REQUEST['maker_id']){
            $maker_id=$_REQUEST['maker_id'];

            $maker_name['nickname']=array('like',"%".trim($maker_id)."%");
            $maker_list=M('follow')->field('id,nickname')->where($maker_name)->select();
            foreach($maker_list as $key=>$val){
                $uids[] = intval($val['id']);
            }
            $map['maker_id']=array('in',$uids);
            $this->assign('maker_id',$maker_id,'','trim');
        }  
        //用户昵称搜索
        if($_REQUEST['user_name']){
            $user_name=$_REQUEST['user_name'];
            $follow_name['nickname']=array('like',"%".trim($user_name)."%");
            $user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
            foreach($user_list as $key=>$val){
                $uid[$key]= $val['id'];
            }
            $map['user_id']=array('in',$uid);
            $this->assign('user_name',$user_name,'','trim');
        }
        //时间搜索
        $start_time=I('get.start_time');
        if($start_time){
            $s_time=intval(strtotime($start_time));
            $map['create_time']=array('gte',$s_time);
            $this->assign('start_time',$start_time);
        }
        $end_time=I('get.end_time');
        if($end_time){
            $e_time=intval(strtotime($end_time))+86400;
            $map['create_time']=array('lte',$e_time);
            $this->assign('end_time',$end_time);
        }
        if($start_time && $end_time){
            $map['create_time']=array('$gte'=>$s_time,'$lte'=>$e_time);
        }
        //分页

        $count = $Model->where($map)->count();
        $Page       = new \Think\Page($count,10);
        $show       = $Page->show();// 分页显示输出
        //查询mongo
        $list = $Model->where($map)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        //获取用户名和代理商名
        foreach($list as $k=>$v){
            //获取用户名.做市商名
            $user = M('follow')->where('id='.$v['user_id'])->field('nickname')->find();
            $maker = M('follow')->where('id='.$v['maker_id'])->field('nickname')->find();
            $list[$k]['user_id'] = $user['nickname'];
            $list[$k]['maker_id'] = $maker['nickname'];
        }
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    /**
     * 导出
     */

    public function download(){
        $map=array();
                //读取配置
        $s_type = C('MONEY_LOG_TYPE');
        $s_types = array_flip($s_type);
        $this->assign('s_types',$s_types);
         $Model = new OptionModel('maker_log');  
        //状态搜索
        if($_REQUEST['status'] && $_REQUEST['status']!=-2){
            $map['money_type']=intval($_REQUEST['status']);
            $status = $_REQUEST['status'];
        }
        else{
            $status=-2;
        }
       $this->assign('status',$status); 
       //做市商名称搜索
        if($_REQUEST['maker_id']){
            $maker_id=$_REQUEST['maker_id'];
            $maker_name['nickname']=array('like',"%".trim($maker_id)."%");

            $maker_list=M('follow')->field('id,nickname')->where($maker_name)->select();
            foreach($maker_list as $key=>$val){
                $uids[] = intval($val['id']);
            }
            $map['maker_id']=array('in',$uids);
            $this->assign('maker_id',$maker_id,'','trim');
        }  
        //用户昵称搜索
        if($_REQUEST['user_name']){
            $user_name=$_REQUEST['user_name'];
            $follow_name['nickname']=array('like',"%".trim($user_name)."%");
            $user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
            foreach($user_list as $key=>$val){
                $uid[$key]= $val['id'];
            }
            $map['user_id']=array('in',$uid);
            $this->assign('user_name',$user_name,'','trim');
        }
        //时间搜索
        $start_time=I('get.start_time');
        if($start_time){
            $s_time=intval(strtotime($start_time));
            $map['create_time']=array('gte',$s_time);
            $this->assign('start_time',$start_time);
        }
        $end_time=I('get.end_time');
        if($end_time){
            $e_time=intval(strtotime($end_time))+86400;
            $map['create_time']=array('lte',$e_time);
            $this->assign('end_time',$end_time);
        }
        if($start_time && $end_time){
            $map['create_time']=array('$gte'=>$s_time,'$lte'=>$e_time);
        }
        $list = $Model->where($map)->order('create_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        //获取用户名和代理商名
        foreach($list as $k=>$v){
            //获取用户名.做市商名
            $user = M('follow')->where('id='.$v['user_id'])->field('nickname')->find();
            $maker = M('follow')->where('id='.$v['maker_id'])->field('nickname')->find();
            $list[$k]['user_id'] = $user['nickname'];
            $list[$k]['maker_id'] = $maker['nickname'];
        }

        $twotime=date("Y-m-d H:i:s");
        $filename=$twotime.'做市商流水资金明细.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $data .= "做市商,";
        $data .= "用户名,";
        $data .= "涉及金额,";
        $data .= "操作类型,";
        $data .= "操作类型ID,";
        $data .= "订单类型,";
        $data .= "用户操作时间,";
        $data .= "说明,";
        $data .= "创建时间,\n";
        if(!empty($list)){
            foreach($list as $k=>$v){
                $data .= $v['maker_id'].",";
                $data .= $v['user_id'].",";
                $data .= $v['money'].",";
                switch ($v['money_type']) {
                    case 1:
                        $money_type  = '下单';
                        break;
                    case 2:
                        $money_type  = '充值';
                        break;
                    case 3:
                        $money_type  = '提现';
                        break;
                    case 4:
                        $money_type  = '佣金';
                        break;
                    case 5:
                        $money_type  = '盈利';
                        break;
                    case 6:
                        $money_type  = '充值手续费';
                        break;
                    case 7:
                        $money_type  = '提现手续费';
                        break;
                    default:
                        $money_type  = '';
                        break;
                }
                $data .= $money_type.","; 
                $data .= $v['option_id'].",";
                switch ($v['order_id_type']) {
                    case 1:
                        $order_id_type  = '螺纹钢类型';
                        break;
                    case 2:
                        $order_id_type  = '恒指类型';
                        break;
                    default:
                        $order_id_type  = '不是订单类型';
                        break;
                }
                $data .= $order_id_type.",";
                $data .=date('Y-m-d H:i:s',$v['option_time'])."\t,";
                $data .= $v['note'].",";         
                $data .=date('Y-m-d H:i:s',$v['create_time'])."\t,\n";
            }
            echo  @mb_convert_encoding($data, 'GB2312','utf-8');
            exit;
        }
    }

    public function all_money(){

    }
}